package com.abkj.platform.util;

public class CRC8
{
  public static int crc16_bit(int crc, byte a)
  {
    byte[] data = new byte[4];
    data[0] = 0;
    data[1] = 0;
    data[2] = 0;
    data[3] = a;
    int temp_a = PublicTool.bytesToInt(data, 0);

    crc ^= temp_a;

    for (int i = 0; i < 8; i++) {
      if ((crc & 0x1) > 0) {
        crc = crc >> 1 ^ 0xA001;
      }
      else {
        crc >>= 1;
      }

    }

    return crc;
  }

  public static int r_crc16_by_bit(byte[] pData, int nBytes)
  {
    int crc = 65535;
    for (int i = 0; i < nBytes; i++) {
      crc = crc16_bit(crc, pData[i]);
    }
    return crc;
  }
}